package com.purong.purong.net;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.purong.purong.Contant;
import com.purong.purong.activity.LoginActivity;
import com.purong.purong.bean.BaseBean;
import com.purong.purong.util.SharePreUtil;
import com.yao.sdk.encrypt.Encrypt;
import com.yao.sdk.net.ResponseInfo;

/**
 * Created by Administrator on 2016/7/20.
 */
public class ResponeHandle {
    public static String isSuccess(Context mContext,ResponseInfo responseInfo){
        responseInfo.content = new Encrypt().toDecryption(responseInfo.content);
        if (responseInfo.content==null)
            return "";
        BaseBean bean = JSON.parseObject(responseInfo.content, BaseBean.class);
        if (bean==null||bean.get_code()==null)
            return "";
            String code=bean.get_code();
        if ((responseInfo.url.equals(Contant.INITIAL)||responseInfo.url.equals(Contant.LOGIN)||responseInfo.url.equals(Contant.SMS_LOGIN)||responseInfo.url.equals(Contant.LOGOUT))&&code.equals("0")){
//            Log.e("zwj",".............new_token="+object.get_t());
            SharePreUtil.getInstance(mContext).setToken(bean.get_t());
        }

            if (code.startsWith("W")){
                Toast.makeText(mContext,bean.get_msg(),Toast.LENGTH_LONG).show();
            }
            if (code.startsWith("E")){
                Toast.makeText(mContext,"网络错误",Toast.LENGTH_LONG).show();
            }
            switch (code){
                case "0":

                    break;
                case "C0001":
                    Toast.makeText(mContext,"网络错误",Toast.LENGTH_LONG).show();
                    Log.e("zwj","参数解析异常");
                    break;
                case "C0002":
                    Toast.makeText(mContext,"网络错误",Toast.LENGTH_LONG).show();
                    Log.e("zwj","token解析错误");
                    break;
                case "C0003":
                    Toast.makeText(mContext,"网络错误",Toast.LENGTH_LONG).show();
                    Log.e("zwj","设备编号错误");
                    break;
                case "C0004":
                    Toast.makeText(mContext,"请登录",Toast.LENGTH_LONG).show();
                    Intent intent=new Intent(mContext,LoginActivity.class);
                    mContext.startActivity(intent);
                    break;
                case "C0005":
                    Toast.makeText(mContext,"用户不存在",Toast.LENGTH_LONG).show();
                    break;
                case "C0006":
                    Toast.makeText(mContext,"用户被拉入黑名单",Toast.LENGTH_LONG).show();
                    break;
                case "C0007":
                    Toast.makeText(mContext,"用户没有权限",Toast.LENGTH_LONG).show();
                    break;
                case "C0008":
                    Toast.makeText(mContext,"网络错误",Toast.LENGTH_LONG).show();
                    Log.e("zwj","参数格式错误");
                    break;
                case "C0009":
                    Toast.makeText(mContext,"网络错误",Toast.LENGTH_LONG).show();
                    Log.e("zwj","参数不存在");
                    break;
            }


        return code;
    }

}
